/**
 * 贪心-加油战-力扣134
 */
public class Practice_2 {
    public static void main(String[] args) {
        int[] gas = {2,3,4};
        int[] cost = {3,4,3};
        int result = canCompleteCircuit(gas, cost);
        System.out.println(result);
    }
    public static int canCompleteCircuit(int[] gas, int[] cost) {
        int curSum=0;
        int totalSum=0;
        int start=0;
        for(int i=0;i<gas.length;i++){//隐含的意思是一圈就能找到start的值。
            curSum=curSum+(gas[i]-cost[i]);
            totalSum=totalSum+(gas[i]-cost[i]);
            if(curSum<0){
                start=i+1;
                curSum=0;
            }
        }
        if(totalSum<0){
            return -1;
        }
        return start;
    }
}
